// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/answer_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/answer"
	answerModel "gin-sns/app/model/module/answer"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var AnswerService = newAnswerService()

func newAnswerService() *answerService {
	return &answerService{}
}

type answerService struct {
}

//根据主键查询数据
func (s *answerService) SelectRecordById(id int64) (*answerModel.Entity, error) {
	return answer.AnswerDao.SelectById(id)
}

//
func (s *answerService) SelectRecordsIn(idarr []int64) ([]answerModel.Entity, error) {

	list, err := answer.AnswerDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *answerService) DeleteRecordById(id int64) bool {
	return answer.AnswerDao.DeleteById(id)
}

//批量删除数据记录
func (s *answerService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := answer.AnswerDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *answerService) AddSave(req *answerModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity answerModel.Entity

	//entity.Id = req.Id
	entity.UserId = req.UserId
	entity.QuestionId = req.QuestionId
	entity.Content = req.Content
	entity.Weight = req.Weight
	entity.CountLike = req.CountLike
	entity.CountUnlike = req.CountUnlike
	entity.CountReward = req.CountReward
	entity.CountFavor = req.CountFavor
	entity.CountShare = req.CountShare
	entity.CountComment = req.CountComment
	entity.Status = req.Status
	entity.LastCommentTime = req.LastCommentTime
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := answer.AnswerDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *answerService) EditSave(req *answerModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &answerModel.Entity{Id: req.Id}
	ok, err := answer.AnswerDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.UserId = req.UserId
	entity.QuestionId = req.QuestionId
	entity.Content = req.Content
	entity.Weight = req.Weight
	entity.CountLike = req.CountLike
	entity.CountUnlike = req.CountUnlike
	entity.CountReward = req.CountReward
	entity.CountFavor = req.CountFavor
	entity.CountShare = req.CountShare
	entity.CountComment = req.CountComment
	entity.Status = req.Status
	entity.LastCommentTime = req.LastCommentTime
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return answer.AnswerDao.Update(entity)
}

//根据条件查询数据
func (s *answerService) SelectListAll(params *answerModel.SelectPageReq) ([]answerModel.Entity, error) {
	return answer.AnswerDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *answerService) SelectListByPage(params *answerModel.SelectPageReq) ([]answerModel.Entity, *page.Paging, error) {
	return answer.AnswerDao.SelectListByPage(params)
}

// 导出excel
func (s *answerService) Export(param *answerModel.SelectPageReq) (string, error) {
	head := []string{"id", "用户id", "回答问题的id", "评论内容", "问题权重", "赞同数量", "反对数量", "打赏数量", "收藏数量", "分享数量", "评论数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "更新时间", "最后评论时间"}
	col := []string{"id", "user_id", "question_id", "content", "weight", "count_like", "count_unlike", "count_reward", "count_favor", "count_share", "count_comment", "status", "create_time", "update_time", "last_comment_time"}
	return answer.AnswerDao.SelectListExport(param, head, col)
}

func (s *answerService) IncrRewardCount(answerId int64) {
	answer.AnswerDao.IncrRewardCount(answerId)
}
func (s *answerService) DecrRewardCount(answerId int64) {
	answer.AnswerDao.DecrRewardCount(answerId)
}

//add
func (s *answerService) IncrLikeCount(answerId int64) {
	answer.AnswerDao.IncrLikeCount(answerId)
}

func (s *answerService) DecrLikeCount(answerId int64) {
	answer.AnswerDao.DecrLikeCount(answerId)
}

func (s *answerService) IncrUnlikeCount(answerId int64) {
	answer.AnswerDao.IncrUnlikeCount(answerId)
}

func (s *answerService) DecrUnlikeCount(answerId int64) {
	answer.AnswerDao.DecrUnlikeCount(answerId)
}
